package com.llcbenwu.controller;

import com.llcbenwu.config.Result;
import com.llcbenwu.config.SecurityProperies;
import com.llcbenwu.pojo.UserDTO;
import com.llcbenwu.util.AppSignUpUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.lang.ref.PhantomReference;

/**
 * @author lilinchun
 * @date 2021/10/22 0022 16:33
 */
@RestController
@Api(value = "用户")
@Slf4j
public class UserController {


    @Autowired
    private ProviderSignInUtils providerSignInUtils;

    @Resource
    private AppSignUpUtils appSignUpUtils;


    @GetMapping("/me")
    @ApiOperation(value = "当前用户信息")
    public Result<Object> getMe() {

        return new Result<>(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
    }
    @GetMapping("/meDetail")
    @ApiOperation(value = "当前用户信息")
    public Result<Object> getMeDetail(@AuthenticationPrincipal UserDetails userDetails) {
        return new Result<>(userDetails);
    }

    @GetMapping("/jwt/meDetail")
    @ApiOperation(value = "当前用户信息")
    public Result<Object> getMeDetail(Authentication  userDetails) {
        return new Result<>(userDetails);
    }


    @GetMapping("/me2")
    @ApiOperation(value = "当前用户信息jwt")
    public Object getCurrentUser2(Authentication authentication, HttpServletRequest request) throws UnsupportedEncodingException {
        //方式2---方式1的简写版
        //从请求头中获取到JWT
        String token = StringUtils.substringAfter(request.getHeader("Authorization"), "Bearer ");
        //借助密钥对JWT进行解析,注意由于JWT生成时编码格式用的UTF-8（看源码可以追踪到）
        //但Jwts工具用到的默认编码格式不是，所以要设置其编码格式为UTF-8
        //(这一步相当于将我们的用户信息装换成了一个Claims)
        Claims claims = Jwts.parser()
                .setSigningKey(SecurityProperies.JWT_SECURITY.getBytes("UTF-8"))
                .parseClaimsJws(token).getBody();
        //取出扩展信息，并打印
        String mes = (String) claims.get("user_name");

        log.info("jwt自定义信息的获取：" +mes);
        return claims;
    }



    @PostMapping("/user/regist")
    @ApiOperation(value = "当前用户信息")
    public void regisUser (UserDTO user, HttpServletRequest request) {
        //不管是注册用户还是绑定用户，都会拿到一个用户唯一标识
        String userId = user.getUserName();
        appSignUpUtils.dosignUp(new ServletWebRequest(request),userId);
    }


    @PostMapping("/user")
    @ApiOperation(value = "用户post")
    public Result<String> getUser() {
        System.out.println("15465789798");
        return new Result<>("测试成功");
    }

    @GetMapping("/user/1")
    @ApiOperation(value = "用户post")
    public Result<String> getUsero() {
        System.out.println("444444444444444444");
        return new Result<>("测试成功");
    }
}
